<?php
namespace  Admin\Controller;
use Think\Controller;
echo '<meta http-equiv=Content-Type content="text/html;charset=utf-8">';
class ExcelBigMoneyController extends IndexController{
    public function money(){
        /** Error reporting */
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
        define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
        date_default_timezone_set('Asia/Shanghai');

        /**select sql echo 查询输出**/
        $ManagerMoneyLog=D("Manager_money_log");
        $where="log.manager_id=manager.id  ";
        $money_info= $ManagerMoneyLog->table('tb_manager_money_log log,tb_manager manager')
            ->where($where)
            ->field("log.id,log.doctor_id,log.money,log.s_money,log.log,manager.shop_id,manager.username,log.action,log.commit_user,log.create_time,log.update_time")
            ->select();

        $result=array();
        $Shop=D("Shop_base");
        $Doctor=D("Doctor");
        foreach($money_info as $key=>$value){
            $result[$key]=$value;
            $shopMap['id']=$value['shop_id'];
            $doctorMap['id']=$value['doctor_id'];
            $result[$key]['bz']=$Shop->where($shopMap)->getField('bz');
            $result[$key]['doctor_id']=$Doctor->where($doctorMap)->getField("name");
            $result[$key]['action']=$value['action']==1?"商户执行了提现操作":"管理员确定了提现操作";
            $result[$key]['papers_type']=$value['papers_type']==0?"身份证":"护照";
            $result[$key]['create_time']=date("Ymd/H:i:s",$result[$key]['create_time']);
            $result[$key]['create_time']=date("Ymd/H:i:s",$result[$key]['update_time']);
            //实到金额
            $result[$key]['true_money']=($value['money']-$value['s_money']);
            unset ($result[$key]['shop_id']);
        }

        //引入外部的类
        import("Vendor.phpexcel.PHPExcels");
        import("Vendor.phpexcel.PHPExcel.CachedObjectStorageFactory",APP_PATH,".php");
        import("Vendor.phpexcel.PHPExcel.Settings",APP_PATH,".php");
        import("Vendor.phpexcel.PHPExcel.IOFactory",APP_PATH,".php");

        $Factory= new \PHPExcel_CachedObjectStorageFactory();
        $Settings= new \PHPExcel_Settings();
        $IOF= new \PHPExcel_IOFactory();
        $cacheMethod = $Factory::cache_in_memory_gzip;
        if ($Settings::setCacheStorageMethod($cacheMethod)) {
            echo date('H:i:s') , " 使用 " , $cacheMethod , " 方式下载文件" , EOL;
        } else {
            echo date('H:i:s') , "将会使用" , $cacheMethod , "默认方式下载文件" , EOL;
        }
// Create new PHPExcel object
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("呱呱试管最初创建")
            ->setLastModifiedBy("呱呱试管最终修改")
            ->setTitle("Office 2007 XLSX 格式的文档")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("格式的文档  Office 2007 XLSX, 使用了PHPExcel.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("文档下载成功");
// Create a first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "ID");
        $objPHPExcel->getActiveSheet()->setCellValue('B1', "商户名");
        $objPHPExcel->getActiveSheet()->setCellValue('C1', "是否有医生提现");
        $objPHPExcel->getActiveSheet()->setCellValue('D1', "提现金额");
        $objPHPExcel->getActiveSheet()->setCellValue('E1', "提现手续费");
        $objPHPExcel->getActiveSheet()->setCellValue('F1', "实到费用");
        $objPHPExcel->getActiveSheet()->setCellValue('G1', "币种");
        $objPHPExcel->getActiveSheet()->setCellValue('H1', "确认管理员");
        $objPHPExcel->getActiveSheet()->setCellValue('I1', "商户提现时间");
        $objPHPExcel->getActiveSheet()->setCellValue('J1', "管理员确认时间");
        // Freeze panes
        $objPHPExcel->getActiveSheet()->freezePane('A4');
        // Rows to repeat at top
        $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
        // Add data
        foreach ($result as $key=>$value) {
            $key+=2;

            $objPHPExcel->getActiveSheet()->setCellValue('A' . $key, $value['id'])
                ->setCellValue('B' . $key, $value['username'])
                ->setCellValue('C' . $key, $value['doctor_id'])
                ->setCellValue('D' . $key, $value['money'])
                ->setCellValue('E' . $key, $value['s_money'])
                ->setCellValue('F' . $key, $value['true_money'])
                ->setCellValue('G' . $key, $value['bz'])
                ->setCellValue('H' . $key, $value['action'])
                ->setCellValue('I' . $key, $value['commit_user'])
                ->setCellValue('J' . $key, $value['create_time'])
                ->setCellValue('K' . $key, $value['update_time'])
                ->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');  //页脚
        }
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
    // Save Excel 2007 file
        echo date('H:i:s') , " 写EXCEL2007格式" , EOL;
        $callStartTime = microtime(true);
        $objWriter = $IOF::createWriter($objPHPExcel, 'Excel2007');
        $path="./Public/Admin/public/excel/商户提现信息/".date('ymdHis')."商户提现信息.xlsx";
        $fileName=iconv('UTF-8','GB2312', $path);
        //保存文件
        $objWriter->save($fileName);
        echo date('H:i:s') , " [用户订单表]下载EXCEL2007格式成功 " , EOL;
    // Echo memory usage
        echo date('H:i:s') , ' 下载大小为: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;
    // Echo done
        echo date('H:i:s') , "  EXCEL下载成功" , EOL;
        header("Location: index.php?m=Admin&c=Index&a=index#");
    }


}
